﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TVKKPR : UIVKKOPF
    {
        #region ==VKPPOS===========
        public static readonly DependencyProperty VKPPOSProperty = DependencyProperty.Register("VKPPOS",
            typeof(string), typeof(TVKKPR),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVKPPOSChanged));
        public string VKPPOS
        {
            get
            {
                return (string)GetValue(VKPPOSProperty);
            }
            set
            {
                SetValue(VKPPOSProperty, value);
            }
        }
        private static void OnVKPPOSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TVKKPR;
            if (src != null)
            {
                src.VKPPOS = (string)e.NewValue;
            }
        }
        #endregion
        public TVKKPR()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVKKOPF();
            this.DataContext = MV;
            InitializeComponent();
            SACHBE = app.User; 
        }
       
        public override void LOADBYAUF()
        { 
            if (ORDERNUM.Length < 13)
                return;
            if (VKPPOS.Length > 8)
            {
                if (VKPPOS.Substring(0, 8) == ORDERNUM.Substring(0, 8))
                {
                    vwMaster.View.MoveCurrentTo(-1);
                    vwMaster.View.MoveCurrentToFirst();
                    VKPPOS = "";
                    VKPPOS = ORDERNUM;
                    return;
                }
            }
            TBMain.Clear(); 
            KRS = ORDERNUM.Substring(0, 2);
            LFD = ORDERNUM.Substring(2, 6);
            AUFART = ORDERNUM.Substring(8, 2);
            KUNDNR = "00000000";
            KURS = "0000000";
            VKKHWAEHRSL = ORDERNUM.Substring(10, 3);
            dwData = TBMain.NewRow();
            dwData["VKKMANDANT"] = app.Mandant;
            dwData["VKKAUFKRS"] = KRS;
            dwData["VKKAUFLFD"] = LFD;
            dwData["VKKSA"] = VKKSA;
            SResult = TMV.QueryVKKOPF(dwData, 4);
            MergeData(SResult, true);
            if (!IsAuto)
                return;
            if (TBMain.Rows.Count > 0)
            {
                vwMaster.View.MoveCurrentTo(-1);
                vwMaster.View.MoveCurrentToFirst();
                VKPPOS = ORDERNUM;
                return;
            }
            if (VKKHWAEHRSL.Trim().Length == 0)
                VKKHWAEHRSL = app.WAEHRSL;
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["VKKMANDANT"] = app.Mandant;
            dwData["VKKAUFART"] = ORDERNUM.Substring(8, 2);
            dwData["VKKSA"] = VKKSA;

            dwData["VKKKUNDNR"] = "00000000";
            dwData["VKKAUFKRS"] = KRS;
            dwData["VKKAUFLFD"] = LFD;
            dwData["VKKSACHBE"] = SACHBE;
            dwData["VKKKURS"] = KURS;
            dwData["VKKHWAEHRSL"] = VKKHWAEHRSL;
            dwData["VKKKURS"] = "0010000";
            dwData["VKKWAEHRSL"] = app.WAEHRSL;//评估价格            
            dwData["VKKKREDKDNR"] = dwData["VKKKUNDNR"];
            dwData["VKKKDARTBEZ"] = "K";
            dwData["VKKBESTDAT"] = MV.date;
            dwData["VKKAEDAT"] = MV.date;
            dwData["VKKBSTDAT"] = MV.date;
            Insert(dwData);
            VKPPOS = ORDERNUM;
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
             base.EditCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            base.SaveCommand(sender, e);
        }
      
    }
}
